<?php

class RssController extends MyController
{
	private $pagesize = 10;
	private $cache_time = 0;
	public function init()
	{
		parent::init();
		header("Content-type: text/xml; charset=utf-8");
	}

	public $meta_keywords;
	public $meta_description;
	public $meta_author;
	/**
	 * 首页
	 */
	public function actionIndex()
	{
		$articles_to_show = Article::model()->findAll("1 order by id desc limit 0,$this->pagesize");
		if(empty($articles_to_show))
		{
			throw new CHttpException(404);
		}
		$this->pageTitle = Sys_config::model()->getconfig('title');
		$this->meta_keywords = Sys_config::model()->getconfig('meta_keywords');
		$this->meta_description = Sys_config::model()->getconfig('meta_desc');
		$this->meta_author = Sys_config::model()->getconfig('author');
		$data = array();
		$data['articles'] = $articles_to_show;
		$data['channel_link'] = '';
		$this->renderPartial('/site/rss',$data);
	}
	
	public function actionCategory()
	{
		//获取分类名称
		$category_name = trim(Yii::app()->request->getParam('category'));
		$cond = new CDbCriteria();
		$cond->condition = 'name=:category';
		$cond->params = array(':category'=>$category_name);
		$category = Article_category::model()->find($cond);
		if(!$category)
		{
			throw new CHttpException(404);
		}
		$article_relations = Article_category_relation::model()->findAll("category_id={$category->id} order by article_id desc limit 0,{$this->pagesize}");
		if(empty($article_relations))
		{
			throw new CHttpException(404);
		}
		$articles_to_show = array();
		foreach ($article_relations as $t)
		{
			if($t->article)
			$articles_to_show[] = $t->article;
		}
		$this->pageTitle = $category_name.'-'.Sys_config::model()->getconfig('title');
		$this->meta_keywords = $category_name.','.Sys_config::model()->getconfig('meta_keywords');
		$this->meta_description = Sys_config::model()->getconfig('meta_desc');
		$this->meta_author = Sys_config::model()->getconfig('author');
		$data = array();
		$data['articles'] = $articles_to_show;
		$data['channel_link'] = '/Category/'.urlencode($category_name);
		$this->renderPartial('/site/rss',$data);
	}
	
	public function actionTag()
	{
		//获取分类名称
		$tag_name = trim(Yii::app()->request->getParam('tag'));
		$cond = new CDbCriteria();
		$cond->condition = 'tag=:tag';
		$cond->params = array(':tag'=>$tag_name);
		$tag = Tag::model()->find($cond);
		if(!$tag)
		{
			throw new CHttpException(404);
		}
		
		$article_tag_relations = Article_tag_relation::model()->findAll("tag_id={$tag->id} order by article_id desc limit 0,{$this->pagesize}");
		if(empty($article_tag_relations))
		{
			throw new CHttpException(404);
		}
		$articles_to_show = array();
		foreach ($article_tag_relations as $t)
		{
			if($t->article)
			$articles_to_show[] = $t->article;
		}
		$this->pageTitle = $tag_name.'-'.Sys_config::model()->getconfig('title');
		$this->meta_keywords = $tag_name.','.Sys_config::model()->getconfig('meta_keywords');
		$this->meta_description = Sys_config::model()->getconfig('meta_desc');
		$this->meta_author = Sys_config::model()->getconfig('author');
		$data = array();
		$data['articles'] = $articles_to_show;
		$data['channel_link'] = '/tag/'.urlencode($tag_name);
		$this->renderPartial('/site/rss',$data);
	}
}